package cn.com.hhrcw.personal.thirdLogin.web;

import cn.com.hhrcw.entity.CmsContent;
import cn.com.hhrcw.entity.SysUser;
import cn.com.hhrcw.personal.thirdLogin.params.KeepAliveParams;
import cn.com.hhrcw.service.ICmsContentService;
import cn.com.hhrcw.service.IMemberThirdService;
import cn.com.hhrcw.thirdLogin.vo.ThridLoginVO;
import cn.com.hhrcw.utils.Constant;
import cn.com.hhrcw.utils.SubjectUtils;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * 保活登录
 * @author lixingxing
 */
@Slf4j
@RestController
@RequestMapping("/personal/keepAlive/")
@Validated
public class KeepAliveController extends JeecgController<CmsContent, ICmsContentService> {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private IMemberThirdService memberThirdService;

    @Value("${spring.profiles.active}")
    private String profiles;

    /**
     * 获取保活token
     *
     * @return
     */
    @PostMapping(value = "/getToken")
    public Result<?> getToken(@RequestBody @Valid KeepAliveParams params) {//

        SysUser sysUser = (SysUser) SubjectUtils.getSubject();
        String sence = params.getSence().toUpperCase().replaceAll("-", "").replaceAll(" ", "").trim();

        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        String token = "KA_" + params.getDayNum() + "_" + sence + uuid;

        ThridLoginVO t = new ThridLoginVO();
        //KA_29_PC21100d07fe3e4ef996a264c81b1d210f
        t.setThirdKey(token);
        t.setThirdSecondKey(token);
        t.setUserId(sysUser.getId());
        t.setThirdType(Constant.LOGIN_TYPE_KEEPALIVE);
        //30 天后过期

        redisTemplate.opsForValue().set(token, t, params.getDayNum(), TimeUnit.DAYS);

        return Result.ok(token);

    }

}


